<?PHP
// vim: set expandtab tabstop=4 shiftwidth=4:
// +----------------------------------------------------------------------+
// | SAPID: XML Sapiens Engine Demonstrator                               |
// +----------------------------------------------------------------------+
// | Author:  Max Baryshnikov aka Mephius <mb@redgraphic.com>,	          |
// | Dmitry Sheiko <sheiko@cmsdevelopment.com>	                		  |
// | Copyright (c) 2004-2005 Max Baryshnikov                              |
// | http://sapid.sourceforge.net	                                      |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+
// Release: 24.02.05 (dd/mm/yy)
// $Id: functions.inc.php,v 1.2 2005/10/31 15:13:14 sheiko Exp $

if (!defined("SAPID_STARTED")) die("Hacking attempt!");

function display_die($template, $locale_env = array()) {
	global $env, $SAPIDTHEME;
		$sapi_adminproc = new sapi_parser($root_path. "usr/themes/".$SAPIDTHEME."/templates/".$template, array_merge($env, $locale_env));
		print ($sapi_adminproc->build_page()); 	
		unset($sapi_adminproc);
		exit;
}


function message_die($error, $title="System note"){
	global $http_path, $SAPIDTHEME, $env;
		$locale_env = array( "window_title" => strip_tags($title),
							 "window_content" => $error );
		$sapi_adminproc = new sapi_parser($root_path. "usr/themes/".$SAPIDTHEME."/templates/system_message.tpl", array_merge($env, $locale_env));
		print ($sapi_adminproc->build_page()); 	
		unset($sapi_adminproc);
		exit;
}

/**
* @return TRUE or error message
* @param string $to_address - recipient address
* @param string $from_address - sender address
* @param string $subject - letter's subject
* @param string $message - letter's message
* @param string $code - charset
* @param string $server - server address
* @desc This function send email to certain address
*/

function send_mail($to_address, $from_address, $subject, $message, $code="windows-1251", $server="") {
	global $HTTP_POST_FILES, $FEEDBACK_EMAIL;
	$message=stripslashes($message);
	if($from_address=="") $from_address = $FEEDBACK_EMAIL;
	/*
	Attach 10 files
	message/rfc822
	*/
	for($i=0;$i<10;$i++)	{
		if($i>0) $suffix=$i; else $suffix="";
	    	if( $fp = @fopen($HTTP_POST_FILES["attachment".$suffix][tmp_name],  "rb") ) {
			$read = @fread($fp, $HTTP_POST_FILES["attachment".$suffix][size]);
			$read = base64_encode($read);
			$read = chunk_split($read);
			$parts[]=array("body"=>$read, "name"=>$HTTP_POST_FILES["attachment".$suffix][name], "c_type"=>$HTTP_POST_FILES["attachment".$suffix][type]);
		}
	}
	$attach_flag=count($parts);
	$boundary="=_".md5(uniqid(time()));
	$headers.="MIME-Version: 1.0\n";
	$headers.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
	$multipart="";
	$multipart.="--$boundary\n";
	if ($code=='w' || $code=='win' || $code=='windows-1251') $code='windows-1251'; 	else $code='koi8-r';
	if($code=='koi8-r'){
		$message=convert_cyr_string($message,'w','k');
		$subject=convert_cyr_string($subject,'w','k');
		$from_address=convert_cyr_string($from_address,'w','k');
	}
	
	$message=trim($message);
   	$multipart.="Content-Type: text/html; charset=$code\n";
	$multipart.="Content-Transfer-Encoding: Quot-Printed\n\n";
	$multipart.="$message\n\n";
	for ($i=(count($parts)-1); $i>=0; $i--){
		$message_part="Content-Type: ".$parts[$i]["c_type"];
		if ($parts[$i]["name"]!="")
			$message_part.="; name = \"".$parts[$i]["name"]."\"\n";
		else
			$message_part.="\n";
		$message_part.="Content-Transfer-Encoding: base64\n";
		$message_part.="Content-Disposition: attachment; filename = \"".$parts[$i]["name"]."\"\n\n";
		$message_part.=$parts[$i]["body"]."\n";
 	$multipart.="--$boundary\n".$message_part;
	}
		$mime = "$multipart--$boundary--\n";
		$headers="From: $from_address\nSubject: $subject\nX-Mailer: REDACTOR_Mail\n$headers\n";
	
	if($server!="")	{
		$fp = fsockopen($server, 25, $GLOBALS[errno], $GLOBALS[errstr], 30);
		if (!$fp)
			 return false;
		fputs($fp,"HELO $server\n");
		fputs($fp,"MAIL FROM: $from_address\n");
		fputs($fp,"RCPT TO: $to_address\n");
		fputs($fp,"DATA\n");
		fputs($fp,$headers);
		if (strlen($headers))
			 fputs($fp,"$headers\n");
		fputs($fp,$mime);
		fputs($fp,"\n.\nQUIT\n");
		while(!feof($fp))
			$resp.=fgets($fp,1024);
		fclose($fp);
		return true;
	} else {
		return @mail($to_address, $subject, $mime, $headers);
	}
	

}



/**
**  XML functions
**/



function getXMLdata($file) {
	global $stack;
	$stack = array();
	$xml_parser = xml_parser_create();
	xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($xml_parser, XML_OPTION_SKIP_WHITE, 1);	

	if(!file_exists($file)) return;
	if($fp = @fopen($file, "r")) {
	while ($dstump = fread($fp, 4096)) {
		$data .= $dstump;
	}
	fclose($fp); } else return;
	xml_parse_into_struct($xml_parser, $data, $values, $tags);
	xml_parser_free($xml_parser);
	
	if($values) {
		foreach($values as $fetch) {
			if($fetch["level"]>1)	{
				if($fetch["level"]>2) {
					$xmldata[$counter][$fetch["tag"]] = $fetch["value"];
					
				}
				if($fetch["level"]==2 and $fetch["type"]=="open") { 
					$counter++;
					if($fetch["attributes"]) {
						foreach($fetch["attributes"] as $k=>$v) {
							$xmldata[$counter]["attribute.".$k] = $v;
						}
						
					}
					
				}
				
			}
		}
	}
	return $xmldata;
}

function putXMLdata($file) {
	global $stack;
	if($stack[0]["children"]) {
		$content = "<".$stack[0]["name"].">\n";
		foreach($stack[0]["children"] as $fetch) {
			$content .= "<".$fetch["name"].">".$fetch["cdata"]."</".$fetch["name"].">\n";
		}
		$content .= "</".$stack[0]["name"].">";
		$fp = fopen($file, "w");
		fwrite($fp, $content);
		fclose($fp);
	}
}



?>